{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "faceListId = \"ljx_635\" #设置人脸列表ID\n",
    "create_facelists_url = \"https://face-ozl.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "subscription_key = \"4f253843772f42e89e959977f6149561\"\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "data = {\n",
    "    \"name\": \"ljx_987\",\n",
    "    \"userData\": \"30人\",\n",
    "    \"recognitionModel\": \"recognition_03\",\n",
    "    \n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 已经成功创建facelist（云端/云计算）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url = \"https://face-ozl.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = 'http://huangjieqi.gitee.io/picture_storage/hjq.jpg'#学生填写\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"huangjieqi\"\n",
    "#学生填写\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '81ea42b0-c471-407f-adf2-6c1746e5af84'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json()#返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "400"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg\",\"丘天惠\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg\",\"林嘉茵\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg\",\"汤玲萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg\",\"曾雯燕\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg\",\"谢依希\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/YuecongYang.png\",\"杨悦聪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuyu19.png\",\"刘宇\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/ltco.jpg\",\"李婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg\",\"黄智毅\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg\",\"黄慧文\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg\",\"张铭睿\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/yichenting.jpg\",\"陈婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/coco022.jpg\",\"洪可凡\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lujizhi.png\",\"卢继志\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg\",\"张梓乐\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '81ea42b0-c471-407f-adf2-6c1746e5af84',\n",
       "   'userData': 'huangjieqi'},\n",
       "  {'persistedFaceId': '27451de9-e21c-460f-b052-bb50997baf86',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '274ed384-8e4d-445a-8322-446145ccab59',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': 'c234edd4-69c0-47d3-88ea-7b093a6b1136',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': '4e0d63b0-77bf-4483-aaac-b697da228d2e',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '0aaba5ca-7800-4b10-9a22-4450757ee6fe',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': '89c36c5a-358a-4b23-9f16-b4be45474c82',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '88136ab2-ac1c-4320-9116-d0498146cbb3',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': 'a6461dc6-0ad6-4eb6-8560-c0eb45c38252',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': 'cc90dc12-c601-4c3e-8d70-ba49cd4125bb',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': '4ffaca7c-6634-425d-bd74-75e2bfec0285',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': '6c6e6b5c-f25f-42a9-948e-e470b5bba154',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': '201e578f-7cca-4633-8e09-dca4dc820f2f',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': 'fbba8844-9602-43fa-9f0b-d74989d7b3a2',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': '0ba7d51b-7789-4da9-a07a-d8c1592fcec2',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '2bbf13ed-5c41-4bda-9e8d-4fae4692b16f',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': 'c6bcf9ea-b678-4857-abd8-3f37469850ec',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': 'a706b2d8-823b-48fb-a584-ca6f2dd8fdff',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': '36c34fd8-d669-4cbc-98c4-00e9f6881797',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': '5585353b-c8f2-4e3f-8721-6da4d3199496',\n",
       "   'userData': '卢继志'}],\n",
       " 'faceListId': 'ljx_635',\n",
       " 'name': 'ljx_987',\n",
       " 'userData': '30人'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url = \"https://api-ljy.cognitiveservices.azure.com/face/v1.0/facelists/{}\" #学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "faceListId = \"ljx_635\"#删除人脸列表 # 学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '81ea42b0-c471-407f-adf2-6c1746e5af84',\n",
       "   'userData': 'huangjieqi'},\n",
       "  {'persistedFaceId': '27451de9-e21c-460f-b052-bb50997baf86',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '274ed384-8e4d-445a-8322-446145ccab59',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': 'c234edd4-69c0-47d3-88ea-7b093a6b1136',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': '4e0d63b0-77bf-4483-aaac-b697da228d2e',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '0aaba5ca-7800-4b10-9a22-4450757ee6fe',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': '89c36c5a-358a-4b23-9f16-b4be45474c82',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '88136ab2-ac1c-4320-9116-d0498146cbb3',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': 'a6461dc6-0ad6-4eb6-8560-c0eb45c38252',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': 'cc90dc12-c601-4c3e-8d70-ba49cd4125bb',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': '4ffaca7c-6634-425d-bd74-75e2bfec0285',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': '6c6e6b5c-f25f-42a9-948e-e470b5bba154',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': '201e578f-7cca-4633-8e09-dca4dc820f2f',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': 'fbba8844-9602-43fa-9f0b-d74989d7b3a2',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': '0ba7d51b-7789-4da9-a07a-d8c1592fcec2',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '2bbf13ed-5c41-4bda-9e8d-4fae4692b16f',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': 'c6bcf9ea-b678-4857-abd8-3f37469850ec',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': 'a706b2d8-823b-48fb-a584-ca6f2dd8fdff',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': '36c34fd8-d669-4cbc-98c4-00e9f6881797',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': '5585353b-c8f2-4e3f-8721-6da4d3199496',\n",
       "   'userData': '卢继志'}],\n",
       " 'faceListId': 'ljx_635',\n",
       " 'name': 'ljx_987',\n",
       " 'userData': '30人'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Detect face 删除列表内人脸id\n",
    "delete_face_url = \"https://api-ljy.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"]\n",
    "# 直接取上面获得的ID{'persistedFaceId': 'f18450d3-60d2-45f3-a69e-783574dc3ce8'} \n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers,params=params_add_face,json={\"url\":img_url})#学生填写\n",
    "r_get_facelist.json()#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Detect face 删除列表内人脸列表\n",
    "delete_face_url = \"https://api-ljy.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# format内直接删除\n",
    "# 注意requests请求为delete\n",
    "r_delete_facelist = requests.delete(delete_face_url.format(faceListId),headers=headers)#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [404]>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_facelist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'fc29b492-1e72-4a2c-821c-011a96b01467',\n",
       "  'faceRectangle': {'top': 264, 'left': 207, 'width': 277, 'height': 277},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 1.7, 'roll': -19.8, 'yaw': -13.9},\n",
       "   'gender': 'male',\n",
       "   'age': 21.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.002,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.969,\n",
       "    'sadness': 0.029,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.44},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.68},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.39},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.18,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.97},\n",
       "     {'color': 'black', 'confidence': 0.9},\n",
       "     {'color': 'red', 'confidence': 0.38},\n",
       "     {'color': 'other', 'confidence': 0.23},\n",
       "     {'color': 'blond', 'confidence': 0.15},\n",
       "     {'color': 'gray', 'confidence': 0.09},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-hjq.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'http://huangjieqi.gitee.io/picture_storage/hjq.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"68548402-46f3-437b-9123-d51bbbe33af8\",#取上方的faceID\n",
    "    \"faceListId\": \"zhichao01\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
